﻿using DataTier;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KlageSystem
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Random rnd = new Random();
            var connStr = ConnectionManager.GetConnStr();


            configureTable(tblAfsluttede);
            configureTable(tblIgang);
            configureTable(tblVentende);

            addDataRows(Org_Klagedata.LoadAll(connStr, KlageStatus.Modtaget), KlageStatus.Modtaget);
            addDataRows(Org_Klagedata.LoadAll(connStr, KlageStatus.Under_behandling), KlageStatus.Under_behandling);
            addDataRows(Org_Klagedata.LoadAll(connStr, KlageStatus.Afsendt), KlageStatus.Afsendt);

        }

        private void addDataRows(List<Org_Klagedata> sagerVentende, KlageStatus status)
        {
            foreach (var klageData in sagerVentende)
            {
                var row = new TableRow();
                row.TableSection = TableRowSection.TableBody;
                var cell = new TableCell();
                cell.Text = klageData.OrgKlagenr.ToString();
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Text = klageData.EjdAdresse;
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Text = klageData.EjdPostnr;
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Text = klageData.EjdBy;
                row.Cells.Add(cell);
                cell = new TableCell();
                var btn = new LinkButton();
                btn.ID = "btn_" + klageData.OrgKlagenr;
                if (status == KlageStatus.Modtaget)
                {
                    btn.Text = "Opret";
                    btn.ToolTip = "Klik for at oprette en e-mail på denne sag";
                    btn.Click += btn_Opret_Click;
                }
                else
                {
                    btn.Text = "Se";
                    btn.ToolTip = "Klik for at se detaljer på denne sage";
                    btn.Click += btn_Se_Click;
                }
                cell.Controls.Add(btn);
                row.Cells.Add(cell);
                switch (status)
                {
                    case KlageStatus.Modtaget:
                        tblVentende.Rows.Add(row);
                        break;
                    case KlageStatus.Under_behandling:
                        tblIgang.Rows.Add(row);
                        break;
                    case KlageStatus.Afsendt:
                    case KlageStatus.Afsluttet:
                        tblAfsluttede.Rows.Add(row);
                        break;
                    case KlageStatus.Slettet:
                    default:
                        break;
                }

            }
        }

        void btn_Se_Click(object sender, EventArgs e)
        {
            var btn = (LinkButton)sender;
            var id = int.Parse(btn.ID.Substring(4));
            Response.Redirect("~/Klage.aspx?s=1&id=" + id);
        }

        void btn_Opret_Click(object sender, EventArgs e)
        {
            var btn = (LinkButton)sender;
            var id = int.Parse(btn.ID.Substring(4));
            Response.Redirect("~/Klage.aspx?s=2&id=" + id);
        }

        private static void configureTable(Table tbl)
        {
            tbl.Rows.Add(getHeader());
            tbl.Width = 450;
            tbl.CssClass = "sagerOverview tablesorter";
            tbl.CellPadding = 0;
            tbl.CellSpacing = 0;
        }

        private static TableHeaderRow getHeader()
        {
            var tblHeader = new TableHeaderRow();
            tblHeader.TableSection = TableRowSection.TableHeader;
            var tblCell = new TableHeaderCell();
            tblCell.Text = "Klagenr";
            tblHeader.Cells.Add(tblCell);
            tblCell = new TableHeaderCell();
            tblCell.Text = "Adresse";
            tblHeader.Cells.Add(tblCell);
            tblCell = new TableHeaderCell();
            tblCell.Text = "Postnr";
            tblHeader.Cells.Add(tblCell);
            tblCell = new TableHeaderCell();
            tblCell.Text = "By";
            tblHeader.Cells.Add(tblCell);
            tblCell = new TableHeaderCell();
            tblCell.Text = "";
            tblCell.Width = 50;
            tblHeader.Cells.Add(tblCell);
            return tblHeader;
        }

        protected void btnUnDelete_Click(object sender, EventArgs e)
        {
            int org_Klagenr = int.MinValue;
            if (txtSagsnr.Text != "")
            {
                if (int.TryParse(txtSagsnr.Text, out org_Klagenr))
                {
                    var connStr = ConnectionManager.GetConnStr();
                    try
                    {
                        var oldKlage = Org_Klagedata.Load(connStr, org_Klagenr);
                        oldKlage.Status = KlageStatus.Under_behandling;
                        oldKlage.Save(connStr);
                    }
                    catch (ArgumentException exp)
                    {
                        txtSagsnr.Text = exp.Message;
                    }
                    Response.Redirect("~/Default.aspx");
                }
            }
        }
    }
}